package com.company.algo.DualPointer;

public class maximum_average_subarray {
    //滑动窗口,注意是固定窗口大小，所以只需要求窗口内的最大和，最后求均值
    public double findMaxAverage(int[] nums, int k) {
        int sum  = 0;
        int n = nums.length;
        for(int i = 0; i<k; i++){
            sum += nums[i];
        }
        int maxSum = sum;
        //sum-左移位+右移位
        for(int i = k; i < n; i++){
            sum = sum - nums[i-k] + nums[i];
            maxSum = Math.max(maxSum, sum);
        }
        return 1.0 * maxSum / k;
    }
}
